package d_serializable1;

import com.sun.source.tree.NewArrayTree;

import java.io.*;
import java.util.ArrayList;

/**
 * @author Cheng-02
 * @version V1.0
 * @Package d_serializable1
 * @date 2024/4/14 下午12:51
 */
public class Test {
    public static void main(String[] args) {

        //write();

        read();

    }

    private static void read() {

        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("module11\\1.txt"));

            try {
                ArrayList<Person> LI = (ArrayList<Person>) objectInputStream.readObject();
                for (Person person : LI) {
                    System.out.println(person);
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void write() {
        try {
            ArrayList<Person> list = new ArrayList<>();

            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("module11\\1.txt", true));
            // ObjectInputStream objectInputStream=new ObjectInputStream( new FileInputStream("module11\\1.txt"));
            Person p1 = new Person(12, "张三");
            Person p2 = new Person(12, "张三");
            Person p3 = new Person(12, "张三");
            list.add(p1);
            list.add(p2);
            list.add(p3);
            objectOutputStream.writeObject(list);//将集合序列化

            objectOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

